package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DeptDTO;
import com.pshcp.domain.model.Dept;
import com.pshcp.domain.model.DoctorDept;
import com.pshcp.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 科室信息表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private DoctorDeptService doctorDeptService;

    public List<Dept> getDeptListByDoctorId(Long doctorId) {
        if (doctorId == null) {
            return Collections.emptyList();
        }
        List<DoctorDept> doctorDeptList = doctorDeptService.getDoctorDeptByDoctorId(doctorId);
        List<Long> deptIds = doctorDeptList.stream().map(DoctorDept::getDeptId).collect(Collectors.toList());
        List<Dept> deptList = this.findByIds(deptIds);
        return deptList;
    }

    public Map<Long, List<Dept>> getDeptListByDoctorIds(List<Long> doctorIds) {
        if (CollectionUtils.isEmpty(doctorIds)) {
            return Collections.emptyMap();
        }
        List<DoctorDept> doctorDeptList = doctorDeptService.getDoctorDeptByDoctorIds(doctorIds);
        if (CollectionUtils.isEmpty(doctorDeptList)) {
            return Collections.emptyMap();
        }
        List<Long> deptIds = doctorDeptList.stream().map(DoctorDept::getDeptId).collect(Collectors.toList());
        List<Dept> deptList = this.findByIds(deptIds);
        Map<Long, Dept> deptMap = deptList.stream().collect(Collectors.toMap(Dept::getId, dept -> dept));
        Map<Long, List<Dept>> result = new HashMap<>();
        for (DoctorDept doctorDept : doctorDeptList) {
            List<Dept> depts = result.get(doctorDept.getDoctorId());
            if (CollectionUtils.isEmpty(depts)) {
                depts = new ArrayList<>();
            }
            Dept dept = deptMap.get(doctorDept.getDeptId());
            if (dept == null) {
                continue;
            }
            depts.add(dept);
            result.put(doctorDept.getDoctorId(), depts);
        }
        return result;
    }


    public PageList<Dept> list(DeptDTO deptDTO) {
        if (deptDTO.getPageNum() != null && deptDTO.getPageSize() != null) {
            PageHelper.startPage(deptDTO.getPageNum(), deptDTO.getPageSize());
        }
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        if (deptDTO.getStatus() != null) {
            criteria.andEqualTo("status", deptDTO.getStatus());
        }
        if (!ObjectUtils.isEmpty(deptDTO.getHisDeptName())) {
            criteria.andLike("hisDeptName", "%" + deptDTO.getHisDeptName() + "%");
        }
        if (!ObjectUtils.isEmpty(deptDTO.getHisSectName())) {
            criteria.andLike("hisSectName", "%" + deptDTO.getHisSectName() + "%");
        }
        if (!CollectionUtils.isEmpty(deptDTO.getIds())){
            criteria.andIn("id", deptDTO.getIds());
        }
        example.setOrderByClause(" created_time desc");
        List<Dept> list = deptMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<Dept> select(DeptDTO deptDTO) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        if (deptDTO.getStatus() != null) {
            criteria.andEqualTo("status", deptDTO.getStatus());
        }
        if (!ObjectUtils.isEmpty(deptDTO.getHisDeptName())) {
            criteria.andLike("hisDeptName", "%" + deptDTO.getHisDeptName() + "%");
        }
        if (!ObjectUtils.isEmpty(deptDTO.getHisSectName())) {
            criteria.andLike("hisSectName", "%" + deptDTO.getHisSectName() + "%");
        }
        if (!CollectionUtils.isEmpty(deptDTO.getIds())){
            criteria.andIn("id", deptDTO.getIds());
        }
        example.setOrderByClause(" sort");
        return deptMapper.selectByExample(example);
    }

    public int insert(Dept dept) {
        dept.setId(SystemUtil.getId());
        dept.setStatus(CommonStatusEnum.INIT.getCode());
        dept.setCreatedTime(new Date());
        return deptMapper.insert(dept);
    }

    public int update(Dept dept) {
        dept.setUpdateTime(new Date());
        return deptMapper.updateByPrimaryKeySelective(dept);
    }

    public Dept findById(Long id) {
        return deptMapper.selectByPrimaryKey(id);
    }

    public List<Dept> findByIds(List<Long> ids) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return deptMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        Dept dept = new Dept();
        dept.setStatus(CommonStatusEnum.DEL.getCode());
        return deptMapper.updateByExampleSelective(dept, example);
    }

}
